package com.sofwin.service.impl.contents;

import com.github.pagehelper.PageHelper;
import com.sofwin.dto.BusinessDto;
import com.sofwin.mapper.CBusinessmodelMapper;
import com.sofwin.mapper.CSalescustomerMapper;
import com.sofwin.mapper.CSupplierMapper;
import com.sofwin.pojo.CBusinessmodelWithBLOBs;
import com.sofwin.pojo.CSalescustomer;
import com.sofwin.pojo.CSupplier;
import com.sofwin.pojo.CSupplierExample;
import com.sofwin.service.contents.CBusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: ssm-parent
 * @description: 商业模式
 * @author: Mr.jhx
 * @create: 2021-04-25 15:17
 * @Version 1.0
 **/
@Service
public class CBusinessServiceImpl implements CBusinessService {
    @Autowired
    private CBusinessmodelMapper businessmodelMapper;
    @Autowired
    private CSupplierMapper supplierMapper;
    @Autowired
    private CSalescustomerMapper salescustomerMapper;
    @Autowired
    private BusinessDto businessDto;
    String firstYear = null;
    String secondYear = null;
    String thirdlyYear = null;


    /**
     * @Description: 商业模式
     * @Author: Mr.jhx
     * @Date: 2021/4/25 17:39
     * @Param: * @param companyId:
     * @Return: * @return: com.sofwin.dto.BusinessDto
     **/
    @Override
    public BusinessDto selectbusinessById(Integer companyId) {
        // 商业模式
        CBusinessmodelWithBLOBs businessmodelWithBLOBs = businessmodelMapper.selectByPrimaryKey(companyId);
        businessDto.setCBusinessmodelWithBLOBs(businessmodelWithBLOBs);
        // 近3年前5大主要供应商采购情况
        selectCsuppilerlimitByYear(businessmodelWithBLOBs.getId());
        // 近3年主要销售前10名客户表
        selectSalescustomerlimitByYear(businessmodelWithBLOBs.getId());
        return businessDto;
    }
    /**
     * @Description:近3年前5大主要供应商采购情况
     * @Author: Mr.jhx
     * @Date: 2021/4/26 17:07
     * @Param: * @param businessId:
     * @Return: * @return: void
     **/
    public void selectCsuppilerlimitByYear(Integer businessId){
        List<CSupplier> cSupplierList = supplierMapper.selectCsuppilerlimitByYear(businessId);
        // 根据时间
        businessDto.setSupplierListFirstYear(new ArrayList<>());
        businessDto.setSupplierListSecondYear(new ArrayList<>());
        businessDto.setSupplierListThirdlyYear(new ArrayList<>());
        for (CSupplier cSupplier:cSupplierList){
            if (firstYear == null || cSupplier.getYear().equals(firstYear)){
                firstYear = cSupplier.getYear();
                businessDto.getSupplierListFirstYear().add(cSupplier);
            }else if (secondYear == null || cSupplier.getYear().equals(secondYear)){
                secondYear = cSupplier.getYear();
                businessDto.getSupplierListSecondYear().add(cSupplier);
            }else if(thirdlyYear == null || cSupplier.getYear().equals(thirdlyYear)){
                thirdlyYear = cSupplier.getYear();
                businessDto.getSupplierListThirdlyYear().add(cSupplier);
            }
        }
    }
    /**
     * @Description:近3年主要销售前10名客户表
     * @Author: Mr.jhx
     * @Date: 2021/4/26 17:09
     * @Param: * @param businessId:
     * @Return: * @return: void
     **/
    public void selectSalescustomerlimitByYear(Integer businessId){
        List<CSalescustomer> cSalescustomerList = salescustomerMapper.selectSalescustomerlimitByYear(businessId);

        // 根据时间

        businessDto.setSalescustomerListFirstYear(new ArrayList<>());
        businessDto.setSalescustomerListSecondYear(new ArrayList<>());
        businessDto.setSalescustomerListThirdlyYear(new ArrayList<>());
        for (CSalescustomer cSalescustomer:cSalescustomerList){
            if (firstYear == null || cSalescustomer.getYear().equals(firstYear)){
                firstYear = cSalescustomer.getYear();
                businessDto.getSalescustomerListFirstYear().add(cSalescustomer);
            }else if (secondYear == null || cSalescustomer.getYear().equals(secondYear)){
                secondYear = cSalescustomer.getYear();
                businessDto.getSalescustomerListSecondYear().add(cSalescustomer);
            }else if(thirdlyYear == null || cSalescustomer.getYear().equals(thirdlyYear)){
                thirdlyYear = cSalescustomer.getYear();
                businessDto.getSalescustomerListThirdlyYear().add(cSalescustomer);
            }
        }

    };
}
